package com.rainkaze.lifeharbor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rainkaze.lifeharbor.R;
import com.rainkaze.lifeharbor.adapter.ToolAdapter;
import com.rainkaze.lifeharbor.databinding.FragmentItemCollectBinding;
import com.rainkaze.lifeharbor.db.DatabaseHelper;
import com.rainkaze.lifeharbor.model.ToolItem;
import com.rainkaze.lifeharbor.utils.ToolRepository;

import java.util.ArrayList;
import java.util.List;

public class CollectItemFragment extends Fragment implements ToolAdapter.OnToolClickListener {

    private FragmentItemCollectBinding binding;
    private ToolAdapter toolAdapter;
    private List<ToolItem> favoriteToolItemList;
    private DatabaseHelper dbHelper;

    public CollectItemFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new DatabaseHelper(getContext());
        favoriteToolItemList = new ArrayList<>();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentItemCollectBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupRecyclerView();
    }

    private void setupRecyclerView() {
        binding.recyclerViewFavoriteTools.setLayoutManager(new LinearLayoutManager(getContext())); // 每行一列
        toolAdapter = new ToolAdapter(getContext(), favoriteToolItemList, this);
        binding.recyclerViewFavoriteTools.setAdapter(toolAdapter);
    }

    private void loadFavoriteTools() {
        favoriteToolItemList.clear();
        List<ToolItem> allPossibleTools = ToolRepository.getBaseToolList();

        for (ToolItem tool : allPossibleTools) {
            if (dbHelper.isToolFavorite(tool.getToolIdentifier())) {
                ToolItem favoriteTool = new ToolItem(
                        tool.getName(),
                        tool.getIconResId(),
                        tool.getTargetActivityClass(),
                        tool.getToolIdentifier()
                );
                favoriteTool.setFavorite(true);
                favoriteToolItemList.add(favoriteTool);
            }
        }

        if (toolAdapter != null) {
            toolAdapter.notifyDataSetChanged();
        }

        updateEmptyStateView();
    }

    private void updateEmptyStateView() {
        if (favoriteToolItemList.isEmpty()) {
            binding.recyclerViewFavoriteTools.setVisibility(View.GONE);
            binding.textViewEmptyFavorites.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerViewFavoriteTools.setVisibility(View.VISIBLE);
            binding.textViewEmptyFavorites.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次 ragment可见时重新加载收藏列表进行刷新
        loadFavoriteTools();
    }

    @Override
    public void onToolClick(ToolItem toolItem, int position) {
        if (toolItem.getTargetActivityClass() != null && getActivity() != null) {
            Intent intent = new Intent(getActivity(), toolItem.getTargetActivityClass());
            getActivity().startActivity(intent);
        } else {
            Toast.makeText(getContext(), "该工具页面尚未配置: " + toolItem.getName(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}